package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.*;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.service.SetmealDishService;
import com.zrrd.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        if (name!=null){
            wrapper.like("name",name);
        }
        wrapper.orderByDesc("create_time");
        Page<Setmeal> pageInfo =new Page<>(page,pageSize);
        setmealService.page(pageInfo,wrapper);
        Page<SetmealDto> pageDto =new Page<>();

        //一个工具类帮助复制对象，必须有相同字段
        //第一个是要传过去的，第二个是要收到的，第三个里排除掉
        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        List<Setmeal> setmeals = pageInfo.getRecords();
        //
        List<SetmealDto> setmealDtoList =new ArrayList<>();
        for(Setmeal setmeal:setmeals){
            SetmealDto setmealDto =new SetmealDto();
            //使用对象拷贝
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category byId = categoryService.getById(setmeal.getCategoryId());
            if (byId!=null){
                setmealDto.setCategoryName(byId.getName());
            }
            setmealDtoList.add(setmealDto);
        }
        pageDto.setRecords(setmealDtoList);
        return R.success(pageDto);
    }
    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmeal(setmealDto);
        return R.success("成功");
    }
    //停售
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status")Integer status, Long[] ids){
        List<Setmeal> setmeals = setmealService.listByIds(Arrays.asList(ids));
        for (Setmeal setmeal : setmeals){
            setmeal.setStatus(status);
        }
        setmealService.updateBatchById(setmeals);
        return R.success("成功");
    }
    /*根据id获取套餐信息*/
    @GetMapping("/{setmealId}")
    public R<SetmealDto> updateById(@PathVariable("setmealId") Long setmealId){
        //根据id获取套餐对象
        Setmeal setmeal = setmealService.getById(setmealId);
        //新建一个dto对象
        SetmealDto dto = new SetmealDto();
        //属性复制
        BeanUtils.copyProperties(setmeal,dto);
        //根据套餐id 查询套餐菜品关系表
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmealId);
        //条件查询
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);

        //将查询到的关联的关系列表 设置给dto
        dto.setSetmealDishes(setmealDishList);
        //返回dto
        return R.success(dto);

    }
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> delete(Long[] ids){
        setmealService.deleteSetmeal(ids);
        return R.success("删除成功");
    }
    //根据分类id查
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "'setmeal_'+#categoryId")
    public R<List<Setmeal>> list(Long categoryId){
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        wrapper.eq("status",1);
        List<Setmeal> setmeals = setmealService.list(wrapper);
        return R.success(setmeals);
    }

    @GetMapping("/dish/{setmealId}")
    public R<List<SetmealDish>> dish(@PathVariable("setmealId") Long setmealId){
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",setmealId);
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);
        return R.success(setmealDishList);
    }

}
